#!/bin/bash

ghdir=/srv/git/upstream/github.com

orgs=$(ls $ghdir | shuf)

i=0
j=0
for d in $orgs
do
	for dd in $ghdir/$d/*/
	do
		f1=$(git -C $dd cat-file -p HEAD:configure.ac 2>/dev/null) && {
			dir="spec-autoconf/$(basename $dd)"
			mkdir -p $dir
			echo "$f1" > $dir/configure.ac

			f2=$(git -C $dd cat-file -p HEAD:Makefile.am 2>/dev/null) &&
			echo "$f2" > $dir/Makefile.am

			let i=i+1
			[ $i -gt 100 ] &&
			[ $j -gt 100 ] && exit

			continue
		}

		f3=$(git -C $dd cat-file -p HEAD:CMakeLists.txt 2>/dev/null) && {
			[ "${f3#*find_}" = "$f3" ] && continue

			dir="spec-cmake/$(basename $dd)"
			mkdir -p $dir
			echo "$f3" > $dir/CMakeLists.txt

			for file in $(git -C $dd ls-tree --full-tree --name-only -r HEAD|grep '\.cmake$')
			do
				f4=$(git -C $dd cat-file -p HEAD:$file 2>/dev/null) && {
					mkdir -p $(dirname $dir/$file)
					echo "$f4" > $dir/$file
				}
			done

			let j=j+1
			[ $j -gt 100 ] && exit
		}
	done
done
